package dao.hibernate;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.MultiMap;
import org.apache.commons.collections.iterators.EntrySetMapIterator;

import util.Util;

import dao.BacSiDAO;
import model.BacSi;

public class BacSiDAOHibernate extends GenericHibernateDAO<BacSi, Long> implements BacSiDAO{

	@Override
	public List<BacSi> findByOneCondition(String criteria, String keyWord) {
		List<BacSi> result = new ArrayList<BacSi>();
		if (criteria.equals("tenBacSi")) {
			result = findByTenBacSiLike(keyWord);
		} else if (criteria.equals("chuyenKhoa")){
			result = findByTenChuyenKhoaLike(keyWord);
		} else if (criteria.equals("noiCongTac")) {
			result = findByTenNoiCongTacLike(keyWord);
		} else if (criteria.equals("ngaySinh")) {
			result = findByNgaySinhLike(keyWord);
		} else if (criteria.equals("thamNien")){
			try {
				int thamNien = Integer.parseInt(keyWord);
				result = findByThamNien(thamNien);
			} catch (NumberFormatException e) {
				//Do nothing
			}
		} else if (criteria.equals("soDienThoai")){
			result = findBySoDienThoaiLike(keyWord);
		}
		return result;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<BacSi> findByNgaySinhLike(String keyWord) {
		return getSession()
		.createQuery(
				"FROM BacSi AS bacSi WHERE bacSi.ngaySinh LIKE :pname")
		.setString("pname", "%" + keyWord + "%").list();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<BacSi> findBySoDienThoaiLike(String keyWord) {
		return getSession()
		.createQuery(
				"FROM BacSi AS bacSi WHERE bacSi.soDienThoai LIKE :pname")
		.setString("pname", "%" + keyWord + "%").list();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<BacSi> findByTenBacSiLike(String keyWord) {
		return getSession()
		.createQuery(
				"FROM BacSi AS bacSi WHERE bacSi.tenChuDe LIKE :pname")
		.setString("pname", "%" + keyWord + "%").list();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<BacSi> findByTenChuyenKhoaLike(String keyWord) {
		return getSession()
		.createQuery(
				"FROM BacSi AS bacSi WHERE bacSi.chuyenKhoa.tenChuyenKhoa LIKE :pname")
		.setString("pname", "%" + keyWord + "%").list();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<BacSi> findByTenNoiCongTacLike(String keyWord) {
		return getSession()
		.createQuery(
				"FROM BacSi AS bacSi WHERE bacSi.noiCongTac.tenChuDe LIKE :pname")
		.setString("pname", "%" + keyWord + "%").list();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<BacSi> findByThamNien(int thamNien) {
		return getSession()
		.createQuery(
				"FROM BacSi AS bacSi WHERE bacSi.thamNien = :pname")
		.setInteger("pname", thamNien).list();
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<BacSi> findByKeyword(String keyWord) {
		return getSession()
				.createQuery(
						"FROM BacSi AS bacSi WHERE bacSi.tenChuDe LIKE :pname OR "
								+ "bacSi.ngaySinh LIKE :pname OR "
								+ "bacSi.chuyenKhoa.tenChuyenKhoa LIKE :pname OR "
								+ "bacSi.thamNien = :pname OR bacSi.noiCongTac.tenChuDe LIKE :pname OR "
								+ "bacSi.noiCongTac.tenChuDe LIKE :pname")
				.setString("pname", "%" + keyWord + "%").list();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<BacSi> findByManyConditions(MultiMap conditions) {
		List<BacSi> result = null;
		EntrySetMapIterator iterator = new EntrySetMapIterator(conditions);
		String criteria = "";
		List<String> keywords = new ArrayList<String>();
		String keyword = "";
		while (iterator.hasNext()) {
			criteria = (String) iterator.next();
			keywords = (ArrayList<String>) conditions.get(criteria);
			for (String k : keywords) {
				keyword = k;
				if (criteria.equals("tenBacSi")) {
					List<BacSi> list = findByTenBacSiLike(keyword);
					result = Util.retainAllBacSi(result, list);
				} else if (criteria.equals("noiCongTac")) {
					List<BacSi> list = findByTenNoiCongTacLike(keyword);
					result = Util.retainAllBacSi(result, list);
				} else if (criteria.equals("tenChuyenKhoa")) {
					List<BacSi> list = findByTenChuyenKhoaLike(keyword);
					result = Util.retainAllBacSi(result, list);
				} else if (criteria.equals("thamNien")) {
					try {
						int thamNien = Integer.parseInt(keyword);
						List<BacSi> list = findByThamNien(thamNien);
						result = Util.retainAllBacSi(result, list);
					} catch (NumberFormatException e) {
						// Do nothing
					}
				}
			}
		}
		return result;

	}

}
